#!/usr/bin/env python
# _*_coding:utf-8 _*_
__author__ = "Gao"
"""画矩形，并自动移动"""

import time
import pygame
import sys
from pygame.locals import *

pygame.init()

screen = pygame.display.set_mode((600,500))
pygame.display.set_caption('画矩形')
pos_x = 300    # 起始点x坐标
pos_y = 200    # 起始点y坐标
vel_x = 2    # x轴移动像素
vel_y = 1    # y轴移动像素

while True:
    for event in pygame.event.get():
        if event.type in (QUIT,KEYDOWN):
            sys.exit()
    screen.fill((0,0,200))

    # 每次循环时移动
    pos_x += vel_x
    pos_y += vel_y

    if pos_x > 500 or pos_x < 0:    # 注意减去矩形自己的长度
        vel_x = -vel_x    # 碰壁后向反方向移动
    if pos_y > 400 or pos_y < 0:
        vel_y = -vel_y    # 碰壁后向反方向移动

    color = 255,255,0
    width = 0
    pos = pos_x, pos_y, 100 ,100    # 起始坐标，x长度，y长度
    pygame.draw.rect(screen,color,pos,width)
    pygame.display.update()
    time.sleep(0.01)